<?php

// Load files
session_start();
require_once('twitterlib/twitteroauth/twitteroauth.php');
require_once('config.php');
require_once('SQLfunc.php');
require_once("func.php");

/* Get user access tokens out of the session. 
 $gotcha indicates if we succesfully got the token and token_secret. */
if( empty($_SESSION['token']) || empty($_SESSION['token_secret'])) 
{
	// Check if we have the cookie and the user is registered on our DB.
	if (!(empty($_COOKIE['token'])))
	{
		$token=$_COOKIE['token'];
		$res = getTokenSecret($token);
		if (empty($res))
		{
			$gotcha = false;
		}
		else
		{	
			$_SESSION['token'] = $token;
			$token_secret = $res['token_secret'];
			$_SESSION['token_secret'] = $token_secret;
			$gotcha=true;
		}
	}
	else
	{
		$gotcha=false;
	}
}
else
{
	$token = $_SESSION['token'];
	$token_secret = $_SESSION['token_secret'];
	setcookie("token", $token, time()+30*24*60*60);
	$gotcha=true;
}

include("includes/header.inc.php");
if(!$gotcha) {
	include("includes/connect.inc.php");
}
else {
	include("includes/favs.inc.php");
}
include("includes/footer.inc.php");
?>
